<?php
class uploadController extends adminController{

    public function upload_config($value) {
        $config=config('UPLOAD');
        return $config[$value];
    }

    
    public function upload_type() {
        $list=$this->upload_config('UPLOAD_TYPE');
        if(empty($list)){
            return $list;
        }
        $list=explode(',', $list);
        $str='';
        foreach ($list as $value) {
            $str.='*.'.$value.';';
        }
        return $str;
    }

	//图片上传
    public function image_upload(){

        $this->id=in($_GET['id']);
        $this->editor=in($_GET['editor']);
        $this->pic=in($_GET['pic']);
        $this->type=$this->upload_type();
        $this->size=intval($this->upload_config('UPLOAD_SIZE'));
        $this->show();
    }

    //文件上传
    public function file_upload(){
        $this->id=in($_GET['id']);
        $this->type=$this->upload_type();
        $this->size=intval($this->upload_config('UPLOAD_SIZE'));
        $this->show();
    }

    //组图上传
    public function images_upload(){

        $this->id=in($_GET['id']);
        $this->type=$this->upload_type();
        $this->size=intval($this->upload_config('UPLOAD_SIZE'));
        $this->show();
    }

    //编辑上传
    public function editor_upload(){
        $this->id=in($_GET['id']);
        $this->type=$this->upload_type();
        $this->size=intval($this->upload_config('UPLOAD_SIZE'));
        $this->show();
    }

    //图片裁剪
    public function image_cotout(){
        $this->id=in($_GET['id']);
        $this->file=in(urldecode($_GET['file']));
        $this->pic=in($_GET['pic']);
        $this->show();
    }

    //获取特定文件
    public function get_list_type(){
        $id=trim(urldecode($_POST['id']),',');
        $ext=urldecode($_POST['ext']);
        if(empty($id)||empty($ext)){
            $this->msg('参数通讯错误！',0);
        }
        $list=model('upload')->get_list_type($id,$ext);
        if(empty($list)){
            $this->msg('暂无文件！',0);
        }   
        $this->msg($list);
    }

    //获取已上传附件
    public function get_files(){
        $list=model('upload')->get_relation_list($_POST['file_id']);
        
        if(!empty($list)){
            $html="";
            foreach ($list as $value) {
                if(!empty($value)){
                    $html.='
                    <tr id="old_'.$value['id'].'">
                      <td style="white-space: nowrap" width="100">'.$value['title'].'.'.$value['ext'].'</td>
                      <td>'.round(intval($value['size']/1024),2).'KB</td>
                      <td><a href="javascript:;" onclick="inseditor(\''.$value['file'].'\',\''.$value['original'].'\',\''.$value['title'].'\',\''.$value['ext'].'\',\''.$value['id'].'\')">[插入]</a> <a href="javascript:;" onClick="delfile(\''.$value['id'].'\')">[删除]</a></td>
                    </tr>
                    ';
                }
            }
        }
        echo $html;
    }

    //删除附件
    public function file_del(){
        $id=intval($_POST['id']);
        $this->alert_str($id,'int',true);
        model('upload')->del($id);
        $this->msg('附件删除完毕');
    }

    public function crop_upload() {
        $pic = $GLOBALS["HTTP_RAW_POST_DATA"];
        $file=urldecode($_GET['file']);
        if(empty($pic)||empty($file)){
            $this->msg('没有发现裁剪图片文件',0);
        }
        //文件路径
        $file_path = ROOT_PATH . 'upload/';
        //文件URL路径
        $file_url = __ROOT__ . '/upload/';
        //文件目录时间
        $filetime=date('Y-m').'/'.date('d');

        $filename=explode('/', $file);
        $filename=end($filename);
        $name=explode('.', $filename);
        $ext=end($name);
        $name=substr($filename,0,-strlen($ext));
        $pinyin = new Pinyin();
        $pattern = '/[^\x{4e00}-\x{9fa5}\d\w]+/u';
        $name = preg_replace($pattern, '', $name);
        $name = substr($pinyin->output($name, true),0,80);
        if(file_exists($file_path.$filetime.'/'.$name.'.'.$ext)){
            $rand='-'.substr(cp_uniqid(),-5);
        }
        $filename=$name.$rand.'.'.$ext;

        $save_dir=$file_path . $filetime . '/';
        $image_url=$file_url . $filetime . '/';
        if(!is_dir($save_dir)){
            if(!@mkdir($save_dir, 0777, true)){
                $this->msg('上传文件夹没有写入权限！',0);
            }
        }
        if(!file_put_contents($save_dir.$filename, $pic)){
            $this->msg('图片裁剪失败！请确认目录有写入权限！',0);
        }
        $size=filesize($save_dir.$filename);
        $array=array('url' => $image_url.$filename, 'original'=>$image_url.$filename,'title'=>$name,'ext'=>$ext,'size'=>$size,);
        $array=$this->upload_data_in($array);
        $this->msg($array,1,true);
    }

    //上传文件
    public function upload_data(){
        $files=$_FILES;
        $ban_ext=array('php','asp','asp','html','htm','js','shtml','txt','aspx');
        if(!empty($files)){
            foreach($files as $file) {
                $name=$file['name'];
                $ext=explode('.', $file['name']);
                $ext=strtolower(end($ext));
                if(in_array($ext, $ban_ext)){
                    $this->msg('非法文件上传！',0,true);
                 return;
                }
            }   
        }else{
            $this->msg('上传文件不能为空！',0,true);
            return;
        }

        //文件路径
        $file_path = ROOT_PATH . 'upload/';
        //文件URL路径
        $file_url = __ROOT__ . '/upload/';
        //文件目录时间
        $filetime=date('Y-m').'/'.date('d');

        //重命名
        function filename(){
            $file=$_FILES['Filedata'];
            $name=explode('.', $file['name']);
            $ext=end($name);
            $name=substr($file['name'],0,-strlen($ext));
            $pinyin = new Pinyin();
            $pattern = '/[^\x{4e00}-\x{9fa5}\d\w]+/u';
            $name = preg_replace($pattern, '', $name);
            $name = substr($pinyin->output($name, true),0,80);
            $file_dir=ROOT_PATH . 'upload/'.date('Y-m').'/'.date('d').'/'.$name.'.'.$ext;
            if(file_exists($file_dir)){
                $rand='-'.substr(cp_uniqid(),-5);
            }
            return $name.$rand;
        }

        //上传
        $config=config('UPLOAD');
        $upload = new UploadFile();
        $upload->maxSize = 1024 * 1024 * $config['UPLOAD_SIZE']; //大小
        $upload->allowExts = explode(',', $config['UPLOAD_TYPE']); //格式
        $upload->savePath = $file_path . $filetime . '/'; //保存路径
        $upload->saveRule = 'filename'; //重命名

        if(!$upload->upload()){
            //输出错误消息
            $this->msg($upload->getErrorMsg(),0,true); 
            return;
        }

        //上传信息
        $info = $upload->getUploadFileInfo();
        $info = $info[0];
        $ext=strtolower($info['extension']);
        $imgext=array('jpg','jpeg','png','gif');

        if(!intval($_GET['config'])){
            $WATERMARK_ON=$_POST['WATERMARK_ON'];
            $WATERMARK_POSITION=$_POST['WATERMARK_POSITION'];
            $THUMBNAIL_ON=$_POST['THUMBNAIL_ON'];
            $THUMBNAIL_WIDTH=$_POST['THUMBNAIL_WIDTH'];
            $THUMBNAIL_HEIGHT=$_POST['THUMBNAIL_HEIGHT'];
            $THUMBNAIL_TYPE=$_POST['THUMBNAIL_TYPE'];
        }else{
            $WATERMARK_ON=$config['WATERMARK_ON'];
            $WATERMARK_POSITION=$config['WATERMARK_POSITION'];
            $THUMBNAIL_ON=$config['THUMBNAIL_ON'];
            $THUMBNAIL_WIDTH=$config['THUMBNAIL_WIDTH'];
            $THUMBNAIL_HEIGHT=$config['THUMBNAIL_HEIGHT'];
            $THUMBNAIL_TYPE=$config['THUMBNAIL_TYPE'];
        }

        //处理默认水印
        if($WATERMARK_ON){
            $waterfile=ROOT_PATH.'/public/watermark/'.$config['WATERMARK_IMAGE'];
            if(file_exists($waterfile)){
                if(in_array($ext, $imgext)){
                    Image::water($file_path.$filetime.'/'.$info['savename'],$waterfile,$WATERMARK_POSITION);
                }
            }
        }
       

        //处理默认缩图
        if($THUMBNAIL_ON){
            if(in_array($ext, $imgext)){
                $thumb=Image::thumb($file_path.$filetime.'/'.$info['savename'],$file_path.$filetime.'/thumb_'.$info['savename'],'',$THUMBNAIL_WIDTH,$THUMBNAIL_HEIGHT,'',$THUMBNAIL_TYPE);
            }
        }
        if($thumb){
            $file=$file_url.$filetime.'/thumb_'.$info['savename'];
        }else{
            $file=$file_url . $filetime . '/'.$info['savename'];
        }

        //返回上传成功后信息
        $title=str_replace('.'.$info['extension'],'', $info['name']);
        $array=array('url' =>$file, 'original'=>$file_url . $filetime . '/'.$info['savename'],'title'=>$title,'ext'=>$ext,'size'=>$info['size']);

        $array=$this->upload_data_in($array);
        $this->msg($array,1,true);      
    }


    public function upload_data_in($array){
        //录入数据库
        $data=array();
        $data['file']=$array['url'];
        $data['original']=$array['original'];
        $data['title']=$array['title'];
        $data['folder']=date('Y-m-d');
        $data['ext']=$array['ext'];
        $data['size']=$array['size'];
        $data['time']=time();
        $id=model('upload')->add($data);
        $array['id']=$id;
        return $array;
    }

}